{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "546e38f7-d2cb-40cb-8cd4-2a9b9795f986",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df=pd.read_csv(\"train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e5b332f-c61e-4848-9e44-cf756a42d51a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#显示Dateframe所有列(参数设置为None代表显示所有行，也可以自行设置数字)\n",
    "pd.set_option('display.max_columns',None)\n",
    "#显示Dateframe所有行\n",
    "pd.set_option('display.max_rows',None)\n",
    "#设置Dataframe数据的显示长度，默认为50\n",
    "pd.set_option('max_colwidth',200)\n",
    "#禁止Dateframe自动换行(设置为Flase不自动换行，True反之)\n",
    "pd.set_option('expand_frame_repr', False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bf04b01-0c04-48e1-b1ec-96a9e0708cee",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看数据集整体情况\n",
    "#df.shape 查看数据规模\n",
    "#df.head()  查看数据前五行\n",
    "#df.dtypes  查看每一列是什么类型\n",
    "#df.describe()  查看每一列的统计值\n",
    "#查看部分字段取值分布\n",
    "for col in train.columns:\n",
    " unique_count=train[col].nunique()\n",
    "if(unique_count<=10):\n",
    " print(train[col].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4557f83-7bbb-4445-b2d7-7234a3c8fb4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#2. 查看数据缺失情况\n",
    "#由于数据采集设备、传输线路故障等机械原因或者记录失误等认为原因，数据缺失通常难以避免，造成缺失的原因主要有以下几种：\n",
    "#数据暂时无法获取\n",
    "#数据在采集过程中被遗漏或丢弃\n",
    "#某些对象的部分特征值不存在\n",
    "#获取数据比较困难 train.isnull().sum()\n",
    "\n",
    "#查看每个字段的缺失值，唯一个数\n",
    "tmp = pd.DataFrame()\n",
    "cols = train.columns\n",
    "tmp['count'] =train[cols].count().values\n",
    "tmp['missing_rate'] = (train.shape[0] - tmp['count'])/train.shape[0]\n",
    "tmp['nunique'] = train[cols].nunique().values\n",
    "tmp.index = cols\n",
    "tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89b2e8b3-42c2-4045-bd8e-679196a90f29",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "#分离数值变运与分类变量\n",
    "Nu_feature = list(train.select_dtypes(exclude=[ 'object']).columns)#数值变量ca_feature = list(train.select_dtypes(include=[ 'object']).columns)\n",
    "#绘制箱线密\n",
    "plt.figure(figsize=(30,30))#箱线型查潘数值型变运异常值i=1\n",
    "for col in Nu_feature:\n",
    "ax=plt.subplot(4,5,i)\n",
    "ax=sns.boxplot(data=train[ col])ax.set_xlabel(col)\n",
    "ax.set_ylabel( 'Frequency \")i+=1\n",
    "plt.show()\n",
    "#结台原始数据及经验，真正的异常值只有umbrell.a_limit这一个变量，有一个-1000000的异常值，但测试集没有，可以忽略不管\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a1aeac5-61b9-4000-b9ec-41061f3e4154",
   "metadata": {},
   "outputs": [],
   "source": [
    "train[ 'fraud' ].value_counts()\n",
    "fraud\n",
    "5191181\n",
    "Name: count, dtype: int64\n",
    "sns.set_context(i'figure.figsize':[6，6]})ax =sns.countplot(x= \"fraud' , data=train)\n",
    "for p, label in zip(ax.patches，train[ 'fraud' ].value_counts().values):\n",
    "ax.annotate( label，(p.get_x()+0.320，p.get_height()))\n",
    "ax.set_title(欺诈行为\\no:无欺诈|1:欺诈\")\n",
    "ax.text(0，300,f' {round(519/len(train),2)*100}%')ax.text(1，300，f'{round(181/len(train),2)*100}%')plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8137384-292c-4284-942d-49a4d09bfc9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")plt.figure(figsize=(30,25))\n",
    "i=1\n",
    "#忽路标签字段\n",
    "Nu_feature_2 = Nu_feature[ 0:-1]for col in Nu_feature_2:\n",
    "ax=plt.subplot(4,5,i)\n",
    "ax=sns.kdeplot(train[ col],color=\"red ' )ax=sns.kdeplot(test[ col],color=' cyan ')ax.set_xlabel(col)\n",
    "ax.set_ylabel( 'Frequency \" )ax=ax.legend([ 'train\" , 'test'])i+=1\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f918fdc3-b719-4158-a533-3c6269b96086",
   "metadata": {},
   "outputs": [],
   "source": [
    "col1=-[ 'policy_state ', 'insured_sex', 'insured_education_level' , \"insured_relationship' , 'incident_type ' , .\n",
    "'incident_state', 'auto_make \"]\n",
    "plt.figure(figsize=(20,10))\n",
    "j=1\n",
    "for col in col1:\n",
    "ax=plt.subplot(4,4,j)\n",
    "ax=plt.scatter(x=range(len(train)),y=train[ col],color='red ')plt.title(col)\n",
    "j+=1\n",
    "k=9\n",
    "for col in col1:\n",
    "ax=plt.subplot(4,4,k)\n",
    "ax=plt.scatter(x=range(len(test)),y=test[col],color='cyan ')plt.title(col)\n",
    "k+=1\n",
    "plt.subplots_adjust(wspace=0.4, hspace=0.3)#澜整图间距plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33da14bb-f3cc-456a-ba80-da6dd248efb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#过舵数值数据,此处为第一次查潘，后续将娄别型数据转换后会进行进一步分析correlation_matrix=train[ Nu_feature].corr()\n",
    "plt.figure(figsize=(12,10))\n",
    "sns.heatmap(correlation_matrix, vmax=0.9,linewidths=0.05,cmap=\"RdGy\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eadc026f-f8e0-434a-bb22-526be6979ca9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字故车辆数分布\n",
    "ns.histplot(train[ ' number_of_vehicles_involved'])plt.yscale('log\"l\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fa03281-1924-46d5-a40c-0ada6feaeb15",
   "metadata": {},
   "outputs": [],
   "source": [
    "#欺诈和正常信况下整体索赔金额分布frauds = train[train.fraud == 1]normal = train[train.fraud == 0]\n",
    "f，(ax1，ax2) = plt.subplots(2,1, sharex=True)\n",
    "bins = 50\n",
    "ax1.hist(frauds.total_claim_amount， bins = bins)ax1.set_title('欺诈\")\n",
    "ax2.hist(normal.total_claim_amount, bins = bins)ax2.set_title( '正常\")\n",
    "plt.xlabel( 'total_claim_amount ($)\")plt.ylabel('事务数')\n",
    "plt.xlim((0，130000))plt.yscale( 'log ')plt.show() ;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ada8ed2-5475-4da5-9c46-742e82f7fb54",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(4,3，figsize=(15,15))train[ 'fraud_str '] = train[ 'fraud' ].astype(str)\n",
    "sns.countplot(data=train，x='policy_state' , hue= 'fraud_str' ,ax=ax[0][0])ax[0][o].set_title('上保险所在地区')\n",
    "sns.countplot(data=train，x='insured_sex ' , hue='fraud_str ', ax=ax[0][1])ax[0][1].set_title('被保人性别\")\n",
    "sns.countplot(data=train，x='insured_education_level' , hue='fraud_str ', ax=ax[0][2])ax[0][2].set_title('被保人学历')\n",
    "sns.countplot(data=train，x='insured_occupation ' , hue='fraud_str', ax=ax[1][0])ax[1][o].set_title('被保人职业')\n",
    "sns.countplot(data=train，x= 'bodily_injuries ' , hue='fraud_str' , ax=ax[1][1])ax[1][1].set_title('身体伤害')\n",
    "sns.countplot(data=train，x='auto_make ' , hue='fraud_str '， ax=ax[1][2])ax[1][2].set_title('汽车品牌')\n",
    "'sns.countplot(data=train，x='insured_hobbies ' , hue='fraud_str' , ax=ax[2][0])ax[2][0].set_title( '被保人兴趣爱好')\n",
    "sns.countplot(data=train，x='insured_relationship' , hue='fraud_str', ax=ax[2][1])ax[2][1].set_title('被保人关系')\n",
    "sns.countplot(data=train，x='incident_type' , hue='fraud_str' , ax=ax[2][2])ax[2][2].set_title('出险类型')\n",
    "sns.countplot(data=train，x='collision_type' , hue='fraud_str '， ax=ax[3][0])ax[3][0].set_title('碰撞类型')\n",
    "sns.countplot(data=train，x='incident_severity ' , hue='fraud_str' , ax=ax[3][1])ax[3][1].set_title('事故严重程度)\n",
    "sns.countplot(data=train，x= 'authorities_contacted', hue='fraud_str' , ax=ax[3][2])ax[3][2].set_title('联系了当地的哪个机构‘)\n",
    "plt.tight_layout()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
